//
//  TSAccountInfoView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/6.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

class TSAccountInfoView: UIView {

    private var avatar : UIImageView!
    private var nameLabel : UILabel!
    private var identifyLabel : UILabel!
    private var storeItem : TSLimitConvertItem!
    private var coinItem : TSLimitConvertItem!
    private var profitItem : TSLimitConvertItem!
    private var gainItem : TSLimitConvertItem!
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func msgAction() {
        
        UIViewController.currentViewController().navigationController?.pushViewController(TSNoticeViewController(), animated: true)
        
    }
    
    func refreshHeaaerView() {
        
        storeItem.setupView(price: "中心钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        
        coinItem.setupView(price: "金币钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.jinbi ?? 0))
        
        profitItem.setupView(price: "盈利钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        
        gainItem.setupView(price: "收益钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
    }
    
    private func configViews() {
        
        let scale : CGFloat = 375 / 120.0
        let containerView = UIImageView.init(image: UIImage(named: "account_bg"))
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(SCREEN_WIDTH / scale)
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = WHITE_COLOR
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        containerView.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(15*T_ScreenScale())
            make.size.equalTo(CGSize(width: 55, height: 55))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
        nameLabel.textColor = WHITE_COLOR
        nameLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        containerView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar.snp.right).offset(10)
            make.top.equalTo(avatar.snp.top).offset(4*T_ScreenScale())
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID：\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = WHITE_COLOR
        identifyLabel.font = UIFont.systemFont(ofSize: 13)
        containerView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(10*T_ScreenScale())
        }
        
        let msgBtn = UIButton.init(type: .system)
        msgBtn.setBackgroundImage(UIImage.init(named: "account_message"), for: .normal)
        msgBtn.addTarget(self, action: #selector(msgAction), for: .touchUpInside)
        msgBtn.setEnlargeEdgeNumber(enlarge: 10)
        self.addSubview(msgBtn)
        msgBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.top.equalTo(15)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        let itemContainer = UIView.init()
        itemContainer.backgroundColor = WHITE_COLOR
        itemContainer.layer.cornerRadius = 10
        itemContainer.layer.masksToBounds = true
        addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-15)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*15, height: 80))
        }
        
        storeItem = createItem(title: "中心钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        itemContainer.addSubview(storeItem)
        
        coinItem = createItem(title: "金币钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.jinbi ?? 0))
        itemContainer.addSubview(coinItem)
        
        profitItem = createItem(title: "盈利钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        itemContainer.addSubview(profitItem)
        
        gainItem = createItem(title: "收益钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        itemContainer.addSubview(gainItem)
        
        let itemArray = [storeItem, coinItem, profitItem, gainItem]
        itemArray.snp.distributeSudokuViews(fixedLineSpacing: 0, fixedInteritemSpacing: 0, warpCount: 2)
        
        let line1 = UIView.init()
        line1.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(5)
            make.centerY.equalToSuperview()
            make.height.equalTo(1)
        }

        let line2 = UIView.init()
        line2.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(5)
            make.centerX.equalToSuperview()
            make.width.equalTo(1)
        }
        
    }
    
    private func createItem(title:String) -> TSLimitConvertItem {
        
        let item = TSLimitConvertItem.init(title)
        return item
    }
    
}

//// MARK: - Initialize
//extension TSAccountInfoView {
//
//    func configViews() {
//
//        bgView = UIView.init()
//        bgView.backgroundColor = BASE_PURPLE_COLOR
//        self.addSubview(bgView)
//        bgView.snp.makeConstraints { (make) in
//            make.left.right.top.equalToSuperview()
//            make.height.equalTo(168+STATUSBAR_HEIGHT)
//        }
//
//        let infoView = UIView.init()
//        infoView.backgroundColor = WHITE_COLOR
//        infoView.layer.cornerRadius = 10
//        infoView.layer.masksToBounds = true
//        self.addSubview(infoView)
//        infoView.snp.makeConstraints { (make) in
//            make.centerX.bottom.equalToSuperview()
//            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*12, height: 139))
//        }
//
//        avatar = UIImageView.init()
//        avatar.image = UIImage(named: "general_avatar_default")
//        avatar.backgroundColor = BASE_PURPLE_COLOR
//        avatar.layer.borderWidth = 1
//        avatar.layer.borderColor = WHITE_COLOR.cgColor
//        avatar.layer.cornerRadius = 30
//        avatar.layer.masksToBounds = true
//        self.addSubview(avatar)
//        avatar.snp.makeConstraints { (make) in
//            make.left.equalTo(44)
//            make.bottom.equalTo(-109)
//            make.size.equalTo(CGSize(width: 60, height: 60))
//        }
//
//        let cardBtn = UIButton.init(type: .custom)
//        cardBtn.setBackgroundImage(UIImage(named: "account_info_bank"), for: .normal)
//        cardBtn.addTarget(self, action: #selector(cardAction), for: .touchUpInside)
//        infoView.addSubview(cardBtn)
//        cardBtn.snp.makeConstraints { (make) in
//            make.top.equalTo(avatar.snp.bottom).offset(5)
//            make.right.equalToSuperview()
//            make.size.equalTo(CGSize(width: 60, height: 22))
//        }
//
//        nameLabel = UILabel.init()
//        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂无记录"
//        nameLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#292929")
//        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
//        infoView.addSubview(nameLabel)
//        nameLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(avatar)
//            make.right.equalTo(cardBtn.snp.left).offset(-10)
//            make.top.equalTo(avatar.snp.bottom).offset(10)
//        }
//
//        identifyLabel = UILabel.init()
//        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
//        identifyLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#292929")
//        identifyLabel.font = UIFont.systemFont(ofSize: 12, weight: .regular)
//        infoView.addSubview(identifyLabel)
//        identifyLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(nameLabel)
//            make.right.equalToSuperview().inset(10)
//            make.top.equalTo(nameLabel.snp.bottom).offset(10)
//        }
//
//        progressView = TSProgressView.init()
//        progressView.backgroundColor = WHITE_COLOR
//        infoView.addSubview(progressView)
//        progressView.snp.makeConstraints { (make) in
//            make.left.right.equalToSuperview()
//            make.top.equalTo(identifyLabel.snp.bottom).offset(15)
//            make.height.equalTo(8)
//        }
//
//        upgradeLabel = UILabel.init()
//        upgradeLabel.attributedText = createAttributedStr(str: "距离升级还需要0流水", attributedStr: "0")
//        upgradeLabel.textAlignment = .center
//        infoView.addSubview(upgradeLabel)
//        upgradeLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(39)
//            make.right.equalToSuperview().inset(39)
//            make.top.equalTo(progressView.snp.bottom).offset(5)
//        }
//
//    }
//
//    func createAttributedStr(str:String, attributedStr:String) -> NSAttributedString {
//        let text = NSString(string: str)
//        let attribute = NSMutableAttributedString.init(string: str)
//
//        attribute.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#A5ABB7"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)], range: NSMakeRange(0, str.count))
//
//        attribute.addAttributes([NSAttributedString.Key.foregroundColor:BASE_PURPLE_COLOR], range: text.range(of: attributedStr))
//        return attribute.copy() as! NSAttributedString
//    }
//}
